using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using AwesomeGame.UserInput;
using AwesomeGame.Screens;
using System.Diagnostics;

namespace AwesomeGame 
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public static GraphicsDeviceManager Graphics;
        public static SpriteBatch SpriteBatch;
        public static ContentManager ContentManager;
        public static GameServiceContainer GameServiceContainer;
        public static GameWindow GameWindow;
        public static GameComponentCollection GameComponents;
        private IScreen _currentScreen;

        public Game1()
        {
            Graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            ContentManager = Content;
        }

        protected override void Initialize()
        {
            this.IsMouseVisible = true;
            GameServiceContainer = this.Services;
            GameWindow = this.Window;
            GameComponents = this.Components;
            Graphics.PreferredBackBufferWidth = 1024;
            Graphics.PreferredBackBufferHeight = 768;
            Graphics.PreferMultiSampling = true;
            Graphics.SynchronizeWithVerticalRetrace = true;
            Graphics.ApplyChanges();

            MainMenu screen = new MainMenu();
            screen.ChangeScreen += new MainMenu.ScreenChangeEvent(screen_ChangeScreen);
            _currentScreen = screen;
            
            base.Initialize();
        }

        void screen_ChangeScreen(IScreen sender, IScreen newScreen)
        {
            _currentScreen = newScreen;
            sender.Dispose();
        }        

        protected override void LoadContent()
        {
            SpriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            _currentScreen.Update(gameTime);

            MouseState state = Mouse.GetState();
            if (Keyboard.GetState().IsKeyDown(Keys.Home))
                Debug.WriteLine(state.X + ", " + state.Y);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, DepthStencilState.Default, RasterizerState.CullNone);
            
            _currentScreen.Draw(gameTime);

            SpriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
